package com.yootk.config;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * ShedLock定时任务配置类
 */
@Configuration
@EnableScheduling   // 启用定时任务
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")    // 30s强制释放锁
public class ShedLockRedisConfig {

    @Value("${spring.profiles.active}")
    private String env;     // 当前应用环境

    @Bean
    public LockProvider lockProvider(RedisConnectionFactory connectionFactory) {
        return new RedisLockProvider(connectionFactory, this.env);
    }
}
